﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class OptionForm : System.Windows.Forms.Form
	{
		public OptionForm()
		{
			base.Icon = Resource.YArchitechIcon;
			this.InitializeComponent();
		}

		public OptionForm(HYHeatingSystemOption heatingOption) : this()
		{
			this.m_Option = heatingOption;
		}

		private void OptionForm_Load(object sender, EventArgs e)
		{
			if (this.m_Option == null)
			{
				this.m_Option = new HYHeatingSystemOption();
				this.m_Option.Load(Product.UserConfigFile);
			}
			this.InitUi();
		}

		private void SetValue(YJKTextBox tb, double dValue)
		{
			string format = "G";
			tb.Text = dValue.ToString(format);
			tb.Tag = dValue;
		}

		private void SetValue(ComboBox cmb, string nValue)
		{
			cmb.Text = nValue;
		}

		private void InitUi()
		{
			this.SetValue(this.hytbDensity, this.m_Option.Density);
			this.SetValue(this.hytbViscosity, this.m_Option.Viscosity * 1000000.0);
			this.SetValue(this.hytbTerminalLoss, this.m_Option.TerminalLoss / 1000.0);
			this.SetValue(this.hytbFrictionalFix, this.m_Option.FrictionFix);
			this.SetValue(this.hytbShockFix, this.m_Option.ShockFix);
			this.SetValue(this.hytbSupplyTemperature, (double)this.m_Option.SupplyTemperature);
			this.SetValue(this.hytbReturnTemperature, (double)this.m_Option.ReturnTemperature);
			this.SetValue(this.hytbPipeMin, this.m_Option.DNRange.Min);
			this.SetValue(this.hytbPipeMax, this.m_Option.DNRange.Max);
			this.SetValue(this.hytbFrictionMin, this.m_Option.FrictionRange.Min);
			this.SetValue(this.hytbFrictionMax, this.m_Option.FrictionRange.Max);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.ReadUIData();
			this.m_Option.Save(Product.UserConfigFile);
			base.DialogResult = DialogResult.OK;
		}

		private void ReadUIData()
		{
			this.m_Option.Density = double.Parse(this.hytbDensity.Text);
			this.m_Option.Viscosity = double.Parse(this.hytbViscosity.Text) / 1000000.0;
			this.m_Option.TerminalLoss = double.Parse(this.hytbTerminalLoss.Text) * 1000.0;
			this.m_Option.FrictionFix = double.Parse(this.hytbFrictionalFix.Text);
			this.m_Option.ShockFix = double.Parse(this.hytbShockFix.Text);
			this.m_Option.SupplyTemperature = int.Parse(this.hytbSupplyTemperature.Text);
			this.m_Option.ReturnTemperature = int.Parse(this.hytbReturnTemperature.Text);
			this.m_Option.DNRange.Set(double.Parse(this.hytbPipeMin.Text), double.Parse(this.hytbPipeMax.Text));
			this.m_Option.FrictionRange.Set(double.Parse(this.hytbFrictionMin.Text), double.Parse(this.hytbFrictionMax.Text));
		}

		private void hytbPipeMin_Validating(object sender, CancelEventArgs e)
		{
			string text = string.Empty;
			if ((sender as YJKTextBox).Text.Trim() == "")
			{
				text = "值不能为空！";
			}
			else if (double.Parse(this.hytbPipeMax.Text.Trim()) <= double.Parse(this.hytbPipeMin.Text.Trim()))
			{
				text = "请检查管径范围的取值，确保前小后大！";
			}
			if (text != string.Empty)
			{
				YJKMessageBox.Information(text);
				e.Cancel = true;
				(sender as YJKTextBox).Undo();
				return;
			}
		}

		public HYHeatingSystemOption m_Option;
	}
}
